package com.bac.rentmap.web.controller.osmimport;

import org.springframework.web.servlet.mvc.SimpleFormController;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.beans.propertyeditors.StringTrimmerEditor;
import org.springframework.validation.BindException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.bac.rentmap.osm.BuildingsReader;
import com.bac.rentmap.model.Building;
import com.bac.rentmap.manager.BuildingManager;

import java.util.List;

/**
 *
 * @author Ivan Holub
 */
public class OsmImportController extends SimpleFormController {

    private BuildingsReader buildingsReader;

    private BuildingManager buildingManager;

    public void setBuildingManager(BuildingManager buildingManager) {
        this.buildingManager = buildingManager;
    }

    public void setBuildingsReader(BuildingsReader buildingsReader) {
        this.buildingsReader = buildingsReader;
    }

    protected Object formBackingObject(HttpServletRequest httpServletRequest) throws Exception {

        int buildingsCount = buildingManager.getBuildingsCount();
        int maxNumber = buildingManager.getMaxNumber(httpServletRequest.getParameter("city"));

        httpServletRequest.setAttribute("buildingsSize", buildingsCount);
        httpServletRequest.setAttribute("maxNumber", maxNumber);

        return super.formBackingObject(httpServletRequest);
    }

    protected ModelAndView onSubmit(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, BindException e) throws Exception {
        String city = httpServletRequest.getParameter("city");

        List<Building> buildings = buildingsReader.readBuildings(getServletContext(), city);

        OsmImportCommand command = (OsmImportCommand) o;

        int number = Integer.valueOf(command.getNumber());

        int maxNumber = buildingManager.getMaxNumber(city);

        int fromIndex = maxNumber;
        int toIndex = maxNumber + number;
        if (fromIndex < buildings.size()) {
            if (toIndex > buildings.size()) {
                toIndex = buildings.size();
            }
            buildings = buildings.subList(fromIndex, toIndex);

            buildingManager.saveBuildings(buildings, city);
        }

        int buildingsCount = buildingManager.getBuildingsCount();
        maxNumber = buildingManager.getMaxNumber(city);

        httpServletRequest.setAttribute("buildingsSize", buildingsCount);
        httpServletRequest.setAttribute("maxNumber", maxNumber);

        return super.onSubmit(httpServletRequest, httpServletResponse, o, e);
    }

    protected void initBinder(HttpServletRequest httpServletRequest, ServletRequestDataBinder binder) throws Exception {
        binder.registerCustomEditor(String.class, new StringTrimmerEditor(false));
    }


}
